//
//  WYHomeController.m
//  Hangbag
//
//  Created by admin on 2018/2/9.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYHomeController.h"
#import "WYHomeHeaderView.h"
#import "WYHomeBrandCell.h"
#import "SDCycleScrollView.h"
#import "WYCommodityController.h"
#import "WYHomeHeaderModel.h"
#import "WYBagListModel.h"
#import "WYBagListModel.h"
#import "WYDailySelectionCell.h"
@interface WYHomeController ()<UITableViewDataSource,UITableViewDelegate,SDCycleScrollViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIView *navBar;
@property (nonatomic,assign)CGFloat offset;
@property (nonatomic,strong)NSMutableArray *headerArray;
@property (nonatomic,strong) WYHomeHeaderView *headerImageView;
@property (nonatomic,strong)NSMutableArray *bagArray;


@end

@implementation WYHomeController
-(NSMutableArray *)bagArray
{
    if (!_bagArray) {
        _bagArray = [[NSMutableArray alloc]init];
    }
    return _bagArray;
}
-(NSMutableArray *)headerArray
{
    if (!_headerArray) {
        _headerArray = [[NSMutableArray alloc]init];
    }
    return _headerArray;
}
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]init];
        _tableView.frame=CGRectMake(0, -20, kScreenWidth, kScreenHeight-64);
        _tableView.delegate=self;
        _tableView.dataSource=self;
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self setNavBar];
    [self loadData];
}

-(void)loadData
{
    WEAKSELF
    [WYHttpServer getHomeListSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        weakSelf.headerArray =[WYHomeHeaderModel getHomeHeaderModelFromJson:response[@"headerimg"]];
        weakSelf.headerImageView.headerDataArray = weakSelf.headerArray;
        weakSelf.bagArray = [WYBagListModel getBagListModelFromJson:response[@"list"]];
        weakSelf.headerImageView.dataArray = weakSelf.bagArray;
        
        
        
        
    }];
}


-(void)setNavBar
{
    UIView *view = [[UIView alloc]init];
    self.navBar=view;
    view.frame = CGRectMake(0, STATUSBAR_HEIGHT, kScreenWidth, 44);
    view.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
    
    [self.view addSubview:view];
    
    UIButton *addressButton = [[UIButton alloc]init];
    [view addSubview:addressButton];
    [addressButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8);
        make.centerY.mas_equalTo(view);
    }];
    
    [addressButton setTitle:@"  北京" forState:UIControlStateNormal];
    [addressButton setImage:[UIImage imageNamed:@"icon_address"] forState:UIControlStateNormal];
    
    UIView *textView = [[UIView alloc]init];
    [view addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(addressButton.mas_right).offset(5);
        make.top.mas_equalTo(5);
        make.bottom.mas_equalTo(-5);
        make.right.mas_equalTo(-8);
    }];
    textView.backgroundColor=[UIColor colorWithWhite:1.0 alpha:0.3];
    
    UIImageView *searchImageView = [[UIImageView alloc]init];
    [textView addSubview:searchImageView];
    
    [searchImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.centerY.mas_equalTo(textView);
        make.width.height.mas_equalTo(20);
    }];
    searchImageView.image=[UIImage imageNamed:@"icon_search"];
    
    
    UITextField *textField = [[UITextField alloc]init];
    [textView addSubview:textField];
    
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(searchImageView.mas_right).offset(5);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(kScreenWidth-addressButton.width-120);
    }];
    textField.placeholder = @"请输入关键词";
    textField.clearButtonMode=UITextFieldViewModeWhileEditing;
}
-(void)setupUI
{
    [self setTableHeaderView];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYHomeBrandCell" bundle:nil] forCellReuseIdentifier:@"WYHomeBrandCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYDailySelectionCell" bundle:nil] forCellReuseIdentifier:@"WYDailySelectionCell"];
    [self.view addSubview:self.tableView];
    
    
}
-(void)setTableHeaderView
{
    WYHomeHeaderView *headerView = [[WYHomeHeaderView alloc]init];
    self.headerImageView = headerView;
    headerView.frame = CGRectMake(0, 0, kScreenWidth, 1200);
    self.tableView.tableHeaderView=headerView;
    headerView.scrollview.delegate=self;
}
#pragma  mark  cycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    WYCommodityController *wcc = [[WYCommodityController alloc]init];
    [self.navigationController pushViewController:wcc animated:YES];
    
    
    NSLog(@"首页 --  点击了%ld张图片",index);
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        WYDailySelectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYDailySelectionCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"WYDailySelectionCell" owner:nil options:nil].firstObject;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    
    WYHomeBrandCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYHomeBrandCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYHomeBrandCell" owner:nil options:nil].firstObject;
    }
    
    
    
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        return 61;
    }
    
    return 155;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.offset = scrollView.contentOffset.y;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView==self.tableView) {
        CGFloat offsetY = scrollView.contentOffset.y;
        if (self.offset>offsetY) {
           self.navBar.alpha = YES;
        }else{
            self.navBar.alpha = NO;
        }
    }
}
@end
